#Ai sp@ce dxg

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class aispace_dxg(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(aispace_dxg,self).__init__("DXG")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.vertInfo = []
        self.coords = []
        self.normals = []
        self.uv = []
        self.weights = []
        
    def make_vertices(self, meshName):
        
        for i in range(len(self.vertInfo)):
            info = self.vertInfo[i]
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, self.coords[info[0]])
            self.add_vert_normals(meshName, i, self.normals[info[1]])
            self.add_vert_uv(meshName, i, self.uv[info[2]])
            
    def parse_vertices(self, numVerts):
        
        for i in range(numVerts):
            self.coords.append(list(self.inFile.read_float(3)))
            
    def parse_normals(self, numNorms):
        
        for i in range(numNorms):
            self.normals.append(list(self.inFile.read_float(3)))
            
    def parse_uv(self, numUV):
        
        for i in range(numUV):
            self.uv.append(list(self.inFile.read_float(2)))
            
    def parse_weights(self, numWeights):
        
        for i in range(numWeights):
            self.weights.append(self.inFile.read_float(2))
    
    def parse_faces(self, meshName, numFaces):
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
    
    def parse_group(self, meshName, numMesh):
        
        #group header
        flagb1, offset_f, flagb2, flagb3, offset_g = self.inFile.read_long(5)
        
        numVerts, numNorms = self.inFile.read_short(2)
        numUV, data_v, numWeights = self.inFile.read_long(3)
        
        for i in range(numMesh):
            self.parse_mesh_info(meshName, i)
            
        self.parse_vertices(numVerts)
        self.parse_normals(numNorms)
        self.parse_uv(numUV)
        
        for i in range(data_v):
            self.inFile.seek(4, 1)
        #self.parse_weights(numWeights)
        
        
        
    def parse_mesh_info(self, meshName, meshNum):
        
        numVerts, numFaces = self.inFile.read_short(2)
        name_t, data_u, vertStart = self.inFile.read_long(3)
        
        print vertStart
        
        for i in range(numVerts):
            info = self.inFile.read_short(5)
            self.vertInfo.append(info)
            
        self.parse_faces(meshName, numFaces)
            
        #for i in range(name_t):
            #charLen = self.inFile.read_long()
            #print self.inFile.read_string(charLen, "\x00")
            
        #data_u
        self.inFile.seek(data_u, 1)
        self.inFile.seek(vertStart + 96)
            
        
    def parse_file(self):
        
        #header
        magic = self.inFile.read_string(4)
        flag1, flag2, flag3, flag4 = self.inFile.read_short(4)
        numMesh = self.inFile.read_long()
        next_data_offset = self.inFile.read_long()
        charCount = self.inFile.read_long()
        meshName = self.inFile.read_string(charCount)
        self.create_object(meshName)
        print numMesh, meshName
        
        self.parse_group(meshName, numMesh)
        self.make_vertices(meshName)
            
def read_file(path):
    
    openFile = StructReader(open(path, 'rb'))
    obj = aispace_dxg(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def definitions():
    
    return "DXG", "DXG", "Aispace DXG"

if __name__ == '__main__':
    
    file1 = "EM_09_006_1_000.dxg"
    read_file(file1)